﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Net;

namespace SkypengerApp.UserWindows
{
    /// <summary>
    /// Logique d'interaction pour Ajouter_Utilis.xaml
    /// </summary>
    public partial class Ajouter_Utilis : Window
    {
        #region VARIABLES & CONSTRUCTEURS

        public delegate void AddUserEventHandler(object sender, AddUserEventArgs e);
        public event AddUserEventHandler OnUserAdded;

        public Ajouter_Utilis()
        {
            InitializeComponent();
        } 

        #endregion

        #region GESTION DES ÉVÈNEMENTS BOUTONS

        private void Btn_Annuler_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Btn_Ajouter_Click(object sender, RoutedEventArgs e)
        {
            string ipAdress = GetIPAddress();
            int port;
            if (ipAdress != null && int.TryParse(TB_Port.Text, out port))
            {
                OnUserAdded(this, new AddUserEventArgs(ipAdress, port, TB_name.Text));
                this.Close();
            }
        } 

        #endregion

        #region MÉTHODES PRIVÉES

        private string GetIPAddress()
        {
            string ip = "0.0.0.0";

            if (TB_IP1.Text != "" && TB_IP2.Text != "" && TB_IP3.Text != "" && TB_IP4.Text != "")
            {
                ip = string.Format("{0}.{1}.{2}.{3}", TB_IP1.Text, TB_IP2.Text, TB_IP3.Text, TB_IP4.Text);                
            }

            return ip;
        } 

        #endregion

        #region CUSTOM EVENTARGS

        public class AddUserEventArgs : EventArgs
        {
            public string IP { get; private set; }
            public int Port { get; private set; }
            public string Name { get; private set; }

            public AddUserEventArgs(string ipAdress, int port, string name)
            {
                IP = ipAdress;
                Port = port;
                Name = name;
            }
        }

        #endregion
    }
}
